//
//  TaskEditViewController.m
//  WorkingResult
//
//  Created by 河野 智遵 on 11/08/15.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <QuartzCore/QuartzCore.h>
#import "TaskEditViewController.h"
#import "ColorHolder.h"
#import "Task.h"

@implementation TaskEditViewController

@synthesize doneActionTarget;
@synthesize doneAction;
@synthesize mode;

- (UIButton *)makePalletButton:(UIColor*) color x:(NSInteger)x y:(NSInteger)y
{
    static NSInteger PALLET_BUTTON_WIDTH = 66;
    static NSInteger PALLET_BUTTON_HEIGHT = 50;
    UIButton* ret = [[UIButton alloc] initWithFrame:CGRectMake(x, y, PALLET_BUTTON_WIDTH, PALLET_BUTTON_HEIGHT)];
    ret.layer.cornerRadius = 6;
    ret.backgroundColor = color;
    
    ColorHolder* colorHolder = [[ColorHolder alloc] initWithColor:color label:colorLabel_];
    [ret addTarget:colorHolder action:@selector(changeLabelColor) forControlEvents:UIControlEventTouchUpInside];
    return ret;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(onCancel)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(onDone)];
    
    self.title = @"タスクの編集";
    
    UILabel* nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 120, 20)];
    nameLabel.text = @"タスクの名前";
    
    nameText_ = [[UITextField alloc] initWithFrame:CGRectMake(10, 35, 300, 30)];
    nameText_.returnKeyType = UIReturnKeyDone;
    nameText_.delegate = self;
    nameText_.borderStyle = UITextBorderStyleRoundedRect;
    nameText_.text = @"aaa";
    
    UILabel* colorLabelLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 120, 20)];
    colorLabelLabel.text = @"色";
    
    colorLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(40, 80, 250, 30)];
    colorLabel_.layer.cornerRadius = 6;
    colorLabel_.backgroundColor = [UIColor orangeColor];
    
    // パレットを並べる
    static NSInteger offset = 78;
    NSInteger x = 10;
    NSInteger y = 160;
    [self.view addSubview:[self makePalletButton:[UIColor colorWithRed:0.5 green:0 blue:0 alpha:1] x: x y:y]];
    x += offset;
    [self.view addSubview:[self makePalletButton:[UIColor colorWithRed:0 green:0.5 blue:0 alpha:1] x:x y:y]];
    x += offset;
    [self.view addSubview:[self makePalletButton:[UIColor colorWithRed:0 green:0 blue:0.5 alpha:1] x:x y:y]];
    x += offset;
    [self.view addSubview:[self makePalletButton:[UIColor colorWithRed:0.5 green:0.5 blue:0 alpha:1] x:x y:y]];
    
    x = 10;
    y += 60;
    [self.view addSubview:[self makePalletButton:[UIColor colorWithRed:0.5 green:0 blue:0.5 alpha:1] x: x y:y]];
    x += offset;
    [self.view addSubview:[self makePalletButton:[UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1] x:x y:y]];
    x += offset;
    [self.view addSubview:[self makePalletButton:[UIColor colorWithRed:0 green:0.5 blue:0.5 alpha:1] x:x y:y]];
    x += offset;
    [self.view addSubview:[self makePalletButton:[UIColor orangeColor] x:x y:y]];
    
    [self.view addSubview:nameLabel];
    [self.view addSubview:nameText_];
    [self.view addSubview:colorLabelLabel];
    [self.view addSubview:colorLabel_];
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)onCancel
{
    [self.view endEditing:YES]; // キーボードを隠すのはpopViewControllerAnimtedより前じゃないとダメ
    [self.navigationController popViewControllerAnimated:YES]; // どのように退場するか、ということをここに書くべきかどうかは、議論の余地がある.
    [self clear];
}

- (void)onDone
{
    NSString* t = [nameText_.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if ([t length] == 0)
    {
        [self onCancel];
        return;
    }
    [self.doneActionTarget performSelector:self.doneAction];
    [self onCancel];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)clear
{
    nameText_.text = @"";
    colorLabel_.backgroundColor = [UIColor colorWithRed:0.5 green:0 blue:0 alpha:1];
}

- (Task*)getTask
{
    return [[[Task alloc] initWithText:nameText_.text color:colorLabel_.backgroundColor] autorelease];
}

- (void)setTask:(Task *)task
{
    nameText_.text = task.text;
    colorLabel_.backgroundColor = task.color;
}

- (void)viewDidUnload
{
    [nameText_ release];
    [colorLabel_ release];
    [super viewDidUnload];
    nameText_ = nil;
    colorLabel_ = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
